<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
    <title>OpenWrt Print&Scan项目</title>
    <link rel="stylesheet" href="./static/iceui/src/ui/ui.css"/>
    <script src="./static/iceui/src/ice.js"></script>
    <script src="./static/iceui/src/ui/ui.js"></script>
	<!-- 引入axios -->
	<script src="./static/axios.js"></script>
</head>
<body>
    <div class="title-l">扫描文件详情</div>
    <!-- 添加面包屑菜单 -->
	<div class="breadcrumb">
		<a href="/">首页</a>
		<a href="/">扫描</a>
		<a href="/SANEScan">文件扫描(SANE方式)</a>
        扫描文件详情
		<a href="/SANEScan" class="back">返回扫描页</a>
	</div>
	<!-- 显示图片，可点击图片放大 -->
	<div style="text-align:center;">
		<img src="/scanData/thumb/{{.filename}}.png" alt="" style="width:300px;" class="img-view">
		<h3>{{.filename}}.pdf</h3>
	</div>
	<br>
	<!-- 添加操作按钮 -->
	<div class="group-form" style="text-align:center;">
		<div class="group-content">
			<button type="button" class="btn btn-ani" onclick="download('{{.filename}}','/scanData/{{.filename}}.pdf')"><span>下载文件<i></i></span></button>
			<button type="button" class="btn btn-ani" onclick="uploadFile2WebDav('{{.filename}}')"><span>上传文件至WebDAV网盘<i></i></span></button>
			<button type="button" class="btn btn-ani" onclick="askForDelete('{{.filename}}')"><span>删除文件<i></i></span></button>
		</div>
	</div>
	<div class="group-form" style="text-align:center;">
		<div class="group-content">
			<button type="button" class="btn btn-ani" onclick="viewPDFOnline('{{.filename}}')"><span>在线预览文件<i></i></span></button>
		</div>
	</div>
	<script>
	// 添加点击事件，点击按钮下载文件
	const download = (filename, url) => {
    let a = document.createElement('a'); 
    a.style = 'display: none'; // 创建一个隐藏的a标签
    a.download = filename;
    a.href = url;
    document.body.appendChild(a);
    a.click(); // 触发a标签的click事件
    document.body.removeChild(a);
   }

   const askForDelete = (filename) => {
	ice.popup({
		content:'是否删除该文件 ?',
		yes: () => askForDelete_yes(filename) // 将函数包装在一个匿名函数内
	})
   }

   const askForDelete_yes = (filename) =>{
	// filename = 'test'
	//删除文件
	console.log("删除文件...")
	//传对象
	axios({
        url: '/remove?filename='+filename,
        method: 'get',
    })
        .then(function (response) {
            console.log("前端收到回复:",response.data)
			if (response.data.status == '200'){
				ice.pop({
					content:'删除文件成功',
				})
			// 等待3秒后跳转到首页
			setTimeout(() => {
				console.log("延时3秒");
				window.location.href = '/SANEScan';
			}, 3000);
			}
        }).catch(function (err) {
            console.log(err)
        });
   }

   const uploadFile2WebDav = (filename) => {
	ice.popup({
		content:'待开发',
	})
   }

   //在线预览文件
   const viewPDFOnline = (filename) => {
		// ice.popup({
		// 	content:'待开发',
		// })
		var Url = "pdfjs/web/viewer.html?file=http://"+getCurrentDomain()+"/scanData/"+filename+".pdf"
		//Url = encodeURIComponent(Url) //防止文件名有中文
        console.log("当前预览文件请求URL:",Url)
		window.location.href = Url;
   }

   //获取当前网页域名
   const getCurrentDomain = () => {
		var domain = window.location.host;
		return domain;
   }

	</script>
</body>
</html>